using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for TableControl.
	/// </summary>
	public class TableControl : System.Windows.Forms.UserControl
  {
    private FreeFlowAdministrator.ExtendedDataGrid tableGrid;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox nameLabel;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label rowsLabel;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public TableControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actions = new UIActionList();
      actions.Add("Refresh", "Refresh the table contents", new ActionExecuteDelegate(RefreshTable));
      actions.Add("Copy to clipboard", "Copy the contents of the table to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
      uiActionsControl.Actions = actions;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.tableGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.panel1 = new System.Windows.Forms.Panel();
      this.rowsLabel = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.nameLabel = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.tableGrid)).BeginInit();
      this.panel1.SuspendLayout();
      this.SuspendLayout();
      // 
      // tableGrid
      // 
      this.tableGrid.AllowNavigation = false;
      this.tableGrid.CaptionVisible = false;
      this.tableGrid.DataMember = "";
      this.tableGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.tableGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.tableGrid.Location = new System.Drawing.Point(0, 48);
      this.tableGrid.Name = "tableGrid";
      this.tableGrid.ReadOnly = true;
      this.tableGrid.Size = new System.Drawing.Size(546, 264);
      this.tableGrid.TabIndex = 0;
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.rowsLabel);
      this.panel1.Controls.Add(this.label2);
      this.panel1.Controls.Add(this.nameLabel);
      this.panel1.Controls.Add(this.label1);
      this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
      this.panel1.Location = new System.Drawing.Point(0, 0);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(546, 48);
      this.panel1.TabIndex = 1;
      // 
      // rowsLabel
      // 
      this.rowsLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.rowsLabel.Location = new System.Drawing.Point(48, 24);
      this.rowsLabel.Name = "rowsLabel";
      this.rowsLabel.Size = new System.Drawing.Size(490, 16);
      this.rowsLabel.TabIndex = 3;
      this.rowsLabel.Text = "rowsLabel";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(8, 24);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(40, 16);
      this.label2.TabIndex = 2;
      this.label2.Text = "Rows:";
      // 
      // nameLabel
      // 
      this.nameLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.nameLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.nameLabel.Location = new System.Drawing.Point(51, 8);
      this.nameLabel.Name = "nameLabel";
      this.nameLabel.ReadOnly = true;
      this.nameLabel.Size = new System.Drawing.Size(487, 13);
      this.nameLabel.TabIndex = 1;
      this.nameLabel.Text = "nameLabel";
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(40, 16);
      this.label1.TabIndex = 0;
      this.label1.Text = "Name:";
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(546, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 312);
      this.uiActionsControl.TabIndex = 2;
      // 
      // TableControl
      // 
      this.Controls.Add(this.tableGrid);
      this.Controls.Add(this.panel1);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "TableControl";
      this.Size = new System.Drawing.Size(696, 312);
      ((System.ComponentModel.ISupportInitialize)(this.tableGrid)).EndInit();
      this.panel1.ResumeLayout(false);
      this.panel1.PerformLayout();
      this.ResumeLayout(false);

    }
		#endregion

    private Table table; 
    public void LoadTable(Table table)
    {
      this.table = table;
      nameLabel.Text = table.Name;
      rowsLabel.Text = table.Data.Rows.Count.ToString();
      tableGrid.DataSource = null;
      tableGrid.DataSource = table.Data;
      tableGrid.AutosizeColumns();
    }

    private string CopyToClipboard()
    {
      tableGrid.CopyToClipboard();
      return null;
    }

    private string RefreshTable()
    {
      table.Refresh();
      LoadTable(table);
      return null;
    }
	}
}
